package com.zhanggm.zyk.auth.utils;

import com.zhenzi.sms.ZhenziSmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
public class ZhenziSmsUtils {

    @Value("${zhenzi.appId}")
    private String appId;
    @Value("${zhenzi.secret}")
    private String secret;
    @Value("${zhenzi.templateId}")
    private String templateId;

    @Value("${zhenzi.apiUrl}")
    private String apiUrl;

    ZhenziSmsClient client = null;


    public ZhenziSmsClient getIntance() {
        if (client == null) {
            client = new ZhenziSmsClient(apiUrl, appId, secret);
        }
        return client;
    }

    /**
     * 发送短信
     * @param to 手机号
     * @param args 短信验证码  过期时间
     * @throws Exception
     */
    public void send(String to, String[] args) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("number", to);
            params.put("templateId", templateId);
       /* String[] templateParams = new String[2];
        templateParams[0] = "3421";
        templateParams[1] = "5分钟";*/
            params.put("templateParams", args);
            String result = getIntance().send(params);
            log.info(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 短信发送
     * @param to 手机号
     * @param vcode 验证码
     * @param timeOut 过期时间
     * @throws Exception
     */
    public void send(String to, String vcode,String timeOut)  {
        String[] args = new String[2];
        args[0] = vcode;
        args[1] = timeOut;
        send(to, args);
    }

}
